package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao {
    private static Teacher[] teas = new Teacher[5];

    static {
        teas[0] = new Teacher("heima001", "柳岩", "23", "1991-11-11");
        teas[1] = new Teacher("heima002", "杨幂", "27", "1997-12-12");
    }

    //库管中添加功能
    public boolean addTeacher(Teacher tea) {
        //使用标记位判断数组容器是否已经装满,假设已经装满
        int index = -1;
        for (int i = 0; i < teas.length; i++) {
            if (teas[i] == null) {
                index = i;
                break;
            }
        }
        //根据index的值返回结果
        if (index == -1) {
            return false;
        } else {
            teas[index] = tea;
            return true;
        }
    }

    //库管中删除功能
    public boolean deleteTeacher(String id) {
        int index = getIndex(id);
        if (index == -1){
            return false;
        }else{
            teas[index] = null;
            return true;
        }
    }

    //库管中修改功能
    public void updateTeacher(String updateId, Teacher tea) {
        int index = getIndex(updateId);
        teas[index] = tea;
    }

    //库管中查看功能
    public Teacher[] findAllTeacher() {
        return teas;
    }

    //根据工号查找元素索引的方法
    public int getIndex(String id) {
        //使用标记位根据工号查找教师在数组容器中的索引,假设工号不存在
        int index = -1;

        for (int i = 0; i < teas.length; i++) {
            if(teas[i]!=null&&teas[i].getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;

    }
}
